#coding=utf-8
import Gadget27
import items
import datetime

def ParseNoticeOld(param, response, item):
    print("Parse Article:" + param["Symbol"] + ":P:" + str(param["Page"]) + ":#:" + str(param["i"]) + ":" + param["Title"] + ":" + response._url)
    pageTitleSel = response.xpath('/html/head/title/text()').extract()
    #return

    noticeTitle = response.xpath('//div[@class="content"]/h4/text()').extract()
    noticeDate = response.xpath('//div[@class="content"]/h5/text()').extract()
    noticeBrief = response.xpath('//div[@class="content"]/pre/text()').extract()
    noticeFileLink = response.xpath('//div[@class="content"]/h4/span/a/@href').extract()

    item["Type"] = "Notice"
    item['Title'] = param["Title"] #noticeTitle[0]
    item["Symbol"] = param["Symbol"]
    item['Link'] = param["Link"]
    item['Category'] = param['Category']
    item['Brief'] = noticeBrief[0]

    item['FileLink'] = None
    if noticeFileLink.__len__() != 0:
        item['FileLink'] = noticeFileLink[0]


    #公告日期：2016-11-15
    #datetimes = noticeDate[0].split("：")
    datetime1 = Gadget27.StringToDateTime(param["DateTime"], "%Y-%m-%d")
    item['DateTime'] = Gadget27.ToDateTimeString(datetime1)
    item['StdDateTime'] = Gadget27.ToUTCDateTime(datetime1)

    return item

def ParseNotice(param, response, item, logger = None):

    s = param["Symbol"] + ":P:" + str(param["Page"]) + ":#:" + str(param["i"]) + ":" + param["Title"] + ":" + response._url
    print("Parse Article : " + s)
    item["Type"] = "Notice"
    item['Title'] = param["Title"] #noticeTitle[0]
    item["Symbol"] = param["Symbol"]
    item['Link'] = param["Link"]
    item['Category'] = param['Category']
    item['DateTime'] = param["DateTime"]
    item['StdDateTime'] = param["StdDateTime"]
    item['StdDateTime2'] = param["StdDateTime2"]

    try:
        pageTitleSel = response.xpath('/html/head/title/text()').extract()
        noticeTitle = response.xpath('//div[@class="detail-header"]/h1/text()').extract()
        #noticeDate = response.xpath('//div[@class="content"]/h5/text()').extract()
        noticeFileLink = response.xpath('//div[@class="detail-header"]/h1/span/a/@href').extract()
        noticeContent = response.xpath('//div[@class="detail-body"]/div/text()').extract()

        item['Content'] = noticeContent[0]
        item['FileLink'] = None
        if noticeFileLink.__len__() != 0:
            item['FileLink'] = noticeFileLink[0]

    except Exception as e:
        print("ParseNotice Failed : " + s)
        if logger != None:
            logger.error(s)

    return item


def ParseSZExchangeBlockTrade(param, response, logger = None):

    list = []
    s = str(param["Page"])
    try:
        # --- 找到表格,其实应该是一个List,先不要extract ---
        tables = response.xpath('//table[contains(@id,"REPORTID_tab1")]')
        # --- xpath IS select whithin Scrapy ---
        rows = tables.xpath('.//tr')  # 注意前面那个点,代表在当前节点内搜索

        headers = []
        for i in range(len(rows)):
            row = rows[i]
            # ---header---
            if i == 0:
                headerItems = row.xpath(".//th")
                for h in headerItems:
                    headers.append(h.xpath(".//text()").extract())
                continue

            # --Content---
            contentItems = row.xpath(".//td")
            # for item in items:
            dt = contentItems[0].xpath(".//text()").extract()
            code = contentItems[1].xpath(".//text()").extract()
            desc = contentItems[2].xpath(".//text()").extract()
            price = contentItems[3].xpath(".//text()").extract()
            volume = contentItems[4].xpath(".//text()").extract()
            money = contentItems[5].xpath(".//text()").extract()
            buySide = contentItems[6].xpath(".//text()").extract()
            sellSide = contentItems[7].xpath(".//text()").extract()

            # ---Create Item---
            #entry = {}
            item = items.BlockTradeItem()
            list.append(item)
            #item["List"] = list

            #---Process datetime---
            dt = dt[0]
            item['DateTime'] = dt
            #把时间变成第二天再处理成标准时间
            dt = Gadget27.StringToDateTime(dt,"%Y-%m-%d")
            dt = dt + datetime.timedelta(days=1)
            item['StdDateTime'] = Gadget27.ToUTCDateTime(dt)
            #---
            item['Symbol'] = code[0] + ".SZ"
            item['Description'] = desc[0]
            item['Price'] = float(price[0])
            item['Volume'] = float(volume[0]) * 10000
            item['Money'] = float(money[0]) * 10000
            item['BuySide'] = buySide[0]
            item['SellSide'] = sellSide[0]
            item['Exchange'] = "SZ"
            item['Type'] = "BlockTrade"

    except:
        print("Parse Block Trade Failed : " + s)
        if logger != None:
            logger.error("Parse Block Trade Failed : " + s)

    return list


#---ShanghaiBlockTrade---
def ParseSSEBlockTrade(param, json, logger = None):

    list = []
    s = str(param["Page"])
    try:
        results = json["result"]
        for ret in results:
            item = items.BlockTradeItem()
            list.append(item)

            item['IfZC'] = ret["ifZc"]
            # ---Process datetime---
            dt = ret["tradedate"]
            item['DateTime'] = dt
            # 把时间变成第二天再处理成标准时间
            dt = Gadget27.StringToDateTime(dt, "%Y-%m-%d")
            dt = dt + datetime.timedelta(days=1)
            item['StdDateTime'] = Gadget27.ToUTCDateTime(dt)
            # ---
            item['Symbol'] = ret["stockid"] + ".SH"
            item['Description'] = ret["abbrname"]
            item['Price'] = float(ret["tradeprice"])
            item['Volume'] = float(ret["tradeqty"]) * 10000
            item['Money'] = float(ret["tradeamount"]) * 10000
            item['BuySide'] = ret["branchbuy"]
            item['SellSide'] = ret["branchsell"]
            item['Exchange'] = "SH"
            item['Type'] = "BlockTrade"

    except Exception as e:
        print(e)
        print("Parse Block Trade Failed : " + s)
        if logger != None:
            logger.error("Parse Block Trade Failed : " + s)

    return list